package com.xkdx.guangguang.fragment.shopunitproduct;

import java.util.HashMap;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.my.pay.PayAction;
import com.xkdx.guangguang.fragment.my.pay.PayFragment;
import com.xkdx.guangguang.fragment.my.pay.PayModule;
import com.xkdx.guangguang.fragment.my.pay.PayPresistence;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.ProductInfo;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class OrderProductInfoFragment extends BaseFragment implements
		OnClickListener {
	private View proview;
	private ProductInfo product;
	private ImageView qrdd, back;
	private TextView name, price, code;
	private PayAction payAction;
	private PayModule payModule;
	private PayPresistence payPresistence;
	private SharePrefenceUtil su;
	private String loginToken;
	private SharePrefenceUtil su2;
	public static boolean isOrderBack = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		product = (ProductInfo) getArguments().getSerializable("orderproduct");
		su = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
		su2 = new SharePrefenceUtil(getActivity(), IConstants.SP_POS);
		loginToken = new SharePrefenceUtil(getActivity(), IConstants.SP_USER)
				.getUserLoginToken();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (isOrderBack) {
			getFragmentManager().popBackStack();
		}
		proview = inflater.inflate(R.layout.shop_unit_order, container, false);
		initViwe(proview);
		setValues();
		return proview;
	}

	/**
	 * 初始化值
	 */
	private void setValues() {
		if (product != null) {
			name.setText(product.getProductName());

			code.setText(product.getProductPosTrace());
			String price1 = String.valueOf(Float.parseFloat(product
					.getProductTotalPrice()) / 100);
			price.setText(price1 + "元");
		}
	}

	private void initViwe(View view) {

		back = (ImageView) view.findViewById(R.id.product_order_back);
		qrdd = (ImageView) view.findViewById(R.id.im_product_qrdd);
		code = (TextView) view.findViewById(R.id.order_code);
		name = (TextView) view.findViewById(R.id.order_name);
		price = (TextView) view.findViewById(R.id.order_totalprice);

		back.setOnClickListener(this);
		qrdd.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.product_order_back:
			getFragmentManager().popBackStack();
			break;
		case R.id.im_product_qrdd: // 确认订单

			String type = su.getVerifyType();

			if (type.equals("2") || type.equals("3")) { // 系统管理员 或商家
														// ,订单号需要mac地址形式
				su2.setPosTrace(code.getText().toString());

			} else { // 普通用户 订单号非mac地址形式
				su2.setNormalTrace(code.getText().toString());
			}

			payAction = new PayAction(loginToken, product.getProductID(),
					su.getUserID(),product.getOrder_Num());
			payModule = new PayModule();
			payPresistence = new PayPresistence(this);
			payPresistence.setActitons(payAction);
			payPresistence.setModule(payModule);
			payPresistence.execute();

			break;

		}
	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOnClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		PayFragment.isBack = false;

		IConstants.POS_FROMWHERE = "danpin";
		IConstants.ProductStatic = product;
		// IConstants.ProductStatic.setProductTotalPrice(product.getProductTotalPrice());

		PayFragment payFragment = new PayFragment();
		Bundle b = new Bundle();
		b.putString("paycode", payModule.CCB_FOR_PAY_URL);
		payFragment.setArguments(b);
		getFragmentManager().beginTransaction()
				.replace(R.id.fragment, payFragment).addToBackStack(null)
				.commit();

	}
}
